package com.ruoyi.system.controller;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.system.domain.SysConfiguration;
import com.ruoyi.system.service.ISysConfigurationService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 配置信息Controller
 * 
 * @author ruoyi
 * @date 2021-04-27
 */
@Controller
@RequestMapping("/system/configuration")
public class SysConfigurationController extends BaseController
{
    private String prefix = "system/configuration";

    @Autowired
    private ISysConfigurationService sysConfigurationService;

    @RequiresPermissions("system:configuration:view")
    @GetMapping()
    public String configuration()
    {
        return prefix + "/configuration";
    }

    /**
     * 查询配置信息列表
     */
    @RequiresPermissions("system:configuration:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(SysConfiguration sysConfiguration)
    {
        startPage();
        List<SysConfiguration> list = sysConfigurationService.selectSysConfigurationList(sysConfiguration);
        return getDataTable(list);
    }

    /**
     * 导出配置信息列表
     */
    @RequiresPermissions("system:configuration:export")
    @Log(title = "配置信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(SysConfiguration sysConfiguration)
    {
        List<SysConfiguration> list = sysConfigurationService.selectSysConfigurationList(sysConfiguration);
        ExcelUtil<SysConfiguration> util = new ExcelUtil<SysConfiguration>(SysConfiguration.class);
        return util.exportExcel(list, "配置信息数据");
    }

    /**
     * 新增配置信息
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增保存配置信息
     */
    @RequiresPermissions("system:configuration:add")
    @Log(title = "配置信息", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(SysConfiguration sysConfiguration)
    {
        return toAjax(sysConfigurationService.insertSysConfiguration(sysConfiguration));
    }

    /**
     * 修改配置信息
     */
    @GetMapping("/edit/{configurationId}")
    public String edit(@PathVariable("configurationId") String configurationId, ModelMap mmap)
    {
        SysConfiguration sysConfiguration = sysConfigurationService.selectSysConfigurationById(configurationId);

        mmap.put("sysConfiguration", sysConfiguration);

        return prefix + "/edit";
    }

    /**
     * 修改保存配置信息
     */
    @RequiresPermissions("system:configuration:edit")
    @Log(title = "配置信息", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(SysConfiguration sysConfiguration)
    {
//        sysConfiguration.setConfigurationMapneedupdate(1);
//        sysConfiguration.setConfigurationSpatneedupdate(1);
//        sysConfiguration.setConfigurationRsineedupdate(1);
        return toAjax(sysConfigurationService.updateSysConfiguration(sysConfiguration));
    }

    /**
     * 删除配置信息
     */
    @RequiresPermissions("system:configuration:remove")
    @Log(title = "配置信息", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(sysConfigurationService.deleteSysConfigurationByIds(ids));
    }
}
